package com.xiaoma.tpo.webservice;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoma.tpo.dao.ReciteWordGroupDao;
import com.xiaoma.tpo.dao.ReciteWordScoreDao;
import com.xiaoma.tpo.domain.ReciteWordGroup;
import com.xiaoma.tpo.domain.ReciteWordScore;
import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.dto.ReciteScoreDto;
import com.xiaoma.tpo.service.ReciteWordService;
import com.xiaoma.tpo.service.UserService;
import com.xiaoma.tpo.utils.TPOUtils;

@Controller
@RequestMapping("/service/recite")
public class ReciteWordWS {

    
    @Autowired
    private ReciteWordService reciteWordService;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private ReciteWordGroupDao groupDao;
    
    @Autowired
    private ReciteWordScoreDao reciteWordScoreDao;
    
    @RequestMapping(value = "/groups", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllGroups(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("groups", groupDao.findAll());
        return result;
        
    }
    
    @RequestMapping(value = "/groups/{groupId}/words", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findGroupWords(@PathVariable Integer groupId){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("words", reciteWordService.getWordsByGroupId(groupId));
        return result;
        
    }
    
    @RequestMapping(value = "/score", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> submitScore(@RequestHeader String token, @RequestBody ReciteScoreDto reciteScoreDto) {
        List<ReciteWordScore> scores = reciteScoreDto.getScores();
        User user = userService.findByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        if (TPOUtils.isAnonymous(user)) {
            result.put("status", 0);
            result.put("message", "anonymous can't access");
            return result;
            
        }
        Date now = new Date();
        if (TPOUtils.over7days(now, user.getLastVisit())) {//大于7天token失效
            result.put("status", -1);
            result.put("message", "token disabled over 7 days, please login");
            return result;
        }
        for (ReciteWordScore item: scores) {
            
            ReciteWordScore entity = new ReciteWordScore();
            entity.setUserId(user.getId());
            entity.setGroupId(item.getGroupId());
            List<ReciteWordScore> wordScores = reciteWordScoreDao.findByColumns(entity);
            ReciteWordScore reciteWordScore = null;
            if (wordScores.size() > 0) {
                reciteWordScore = wordScores.get(0);
                if (reciteWordScore.getScore() < item.getScore()) {
                    reciteWordScore.setScore(item.getScore());
                    reciteWordScoreDao.update(reciteWordScore);
                }
            } else {
                reciteWordScore = new ReciteWordScore();
                reciteWordScore.setCreatedAt(new Date());
                reciteWordScore.setGroupId(item.getGroupId());
                reciteWordScore.setScore(item.getScore());
                reciteWordScore.setUserId(user.getId());
                reciteWordScoreDao.insert(reciteWordScore);
            }
        }
        
        result.put("status", 1);
        return result;
    }
    
}
